package tv.accedo.achi.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import tv.accedo.achi.dao.AppDao;
import tv.accedo.achi.dao.UserDao;
import tv.accedo.achi.model.App;
import tv.accedo.achi.model.User;

/**
 * The business logic service of user relevant functionality
 * @author Bo Liang
 *
 */
@Component
public class UserService {
	private User user; 
	private UserDao userDao;
	private AppDao appDao;
	private App app;
	
	/**
	 * Get all applications that make the one user unlocked with state of true.
	 * @param username	user name
	 * @return			JSON format apps
	 */
	public String getJsonAppsnsByUsername(String username) {
		List<String> appsnList = new ArrayList<String>(); 
		List<App> appList = new ArrayList<App>(); 
		String appsnsJson = "]}"; 
		String state = "none"; 
		user = userDao.getUserByUsername(username);
		
		// get the app's sn list which user already have
		appsnList = userDao.getAppsnsByUseid(user.getId()); 
		
		// get all applications
		appList = appDao.getAllApps(); 
		
		for(Iterator<App> iter = appList.iterator(); iter.hasNext(); ) {
			app = iter.next();
			// if this user already unlocked the achievement, make state to true
			if (appsnList.contains(app.getApp_sn())) {
				int appNum = appDao.getAppNumByAppsn(app.getApp_sn());
				int userAppNum = userDao.getAppNumByUseridAndAppsn(user.getId(), app.getApp_sn());
				if (userAppNum < appNum) {
					state = "playing";
				} else {
					state = "finished";
				}			 
			} else {
				state = "none"; 	
			}
			appsnsJson = "{appsn:'" + app.getApp_sn() + "',appname:'" + app.getApp_name() + "',state:'" + state + "'}," + appsnsJson;
					
		}
		
		appsnsJson = "{appsns:[" + appsnsJson; 
		
		try {
			Thread.sleep(1200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return appsnsJson; 
	}
	
	public String getJsonAppsnsByUsernameForFriendShow(String username) {
		List<String> appsnList = new ArrayList<String>(); 
		List<App> appList = new ArrayList<App>(); 
		String appsnsJsonTail = "]}"; 
		String appsnsJsonFinished = "";
		String appsnsJsonPlaying = "";
		String appsnsJsonNone = "";
		String appsnsJson;
		String state = "none"; 
		user = userDao.getUserByUsername(username);
		
		// get the app's sn list which user already have
		appsnList = userDao.getAppsnsByUseid(user.getId()); 
		
		// get all applications
		appList = appDao.getAllApps(); 
		
		for(Iterator<App> iter = appList.iterator(); iter.hasNext(); ) {
			app = iter.next();
			// if this user already unlocked the achievement, make state to true
			if (appsnList.contains(app.getApp_sn())) {
				int appNum = appDao.getAppNumByAppsn(app.getApp_sn());
				int userAppNum = userDao.getAppNumByUseridAndAppsn(user.getId(), app.getApp_sn());
				if (userAppNum < appNum) {
					state = "playing";
					appsnsJsonPlaying = "{appsn:'" + app.getApp_sn() + "',appname:'" + app.getApp_name() + "',state:'" + state + "'}," + appsnsJsonPlaying;
				} else {
					state = "finished";
					appsnsJsonFinished = "{appsn:'" + app.getApp_sn() + "',appname:'" + app.getApp_name() + "',state:'" + state + "'}," + appsnsJsonFinished;
				}	
			} else {
				state = "none"; 
				appsnsJsonNone = "{appsn:'" + app.getApp_sn() + "',appname:'" + app.getApp_name() + "',state:'" + state + "'}," + appsnsJsonNone;
			}
					
		}
		
		appsnsJson = "{appsns:[" + appsnsJsonFinished + appsnsJsonPlaying + appsnsJsonNone + appsnsJsonTail; 
		
		try {
			Thread.sleep(1200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return appsnsJson; 
	}
	
	/**
	 * Get user's friends.
	 * @param username	user name
	 * @return			JSON friends
	 */
	public String getJsonFriendsByUsername(String username) {
		List<Integer> fri_idList = new ArrayList<Integer>(); 
		int fri_id; 
		String jsonFriends = "]}"; 
		
		user = userDao.getUserByUsername(username); 
		fri_idList = userDao.getFriendsidListByUserid(user.getId()); 
		
		for (Iterator<Integer> iter = fri_idList.iterator(); iter.hasNext(); ) {
			fri_id = iter.next(); 
			user = userDao.getUserByUserid(fri_id); 
			jsonFriends = "{friend_name:'" + user.getUsername() + "', friend_id:'" + user.getId() + "'},"
					+ jsonFriends;
		}
		
		jsonFriends = "{friends:[" + jsonFriends;
		return jsonFriends; 
		
	}
	
	/**
	 * Search the users by using inputString with fuzzy query
	 * @param username		user name
	 * @param inputString	the input string for fuzzy query
	 * @return				JSON format users
	 */
	public String getJsonNoneFriendsByInputString(String username, String inputString) {
		List<User> userList = new ArrayList<User>();
		List<Integer> friendIdList = new ArrayList<Integer>();
		String jsonFriends = "]}"; 
		user = userDao.getUserByUsername(username);
		// make a user list that contains user and user's friends
		friendIdList = userDao.getFriendsidListByUserid(user.getId());
		friendIdList.add(user.getId());
		
		userList = userDao.getUsersByString(inputString);
		
		for (Iterator<User> iter = userList.iterator(); iter.hasNext(); ) {
			user = iter.next();
			// user itself and user's friend can not plus into the jsonFriends
			if (!friendIdList.contains(user.getId())) {
				jsonFriends = "{friend_name:'" + user.getUsername() + "', friend_id:'" + user.getId() + "'},"
							+ jsonFriends;
			}
		}
		jsonFriends = "{friends:[" + jsonFriends;
		
		try {
			Thread.sleep(1200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return jsonFriends; 	
	}
	
	
	/**
	 * Fetch one specific user's score value.
	 * @param username 	user name
	 * @return 			user score
	 */
	public long getScoreByUsername(String username) {
		long score = -1; 
		user = userDao.getUserByUsername(username); 
		if (user != null) score = user.getScore(); 
		return score; 
	}
	
	/**
	 * fetch one specific user's all information.
	 * @param username 	user name
	 * @return 			JSON format user
	 */
	public String getJsonUserByUsername(String username) {
		String jsonUser = null;
		user = userDao.getUserByUsername(username); 
		jsonUser = "{userid:'" 
			+ user.getId()
			+ "', score:'"
			+ user.getScore()
			+ "', level:'"
			+ user.getLevel()
			+ "', username:'"
			+ user.getUsername()
			+ "'}";
		return jsonUser; 
	}
	
	/**
	 * Get all users score, return in the format of Json.
	 * @return all users score in Json
	 */
	public String getAllJsonUsersScore() {
		String jsonUsers = "]}"; 
		List<User> userList = new ArrayList<User>(); 
		userList = userDao.getAllUsers(); 
		for(Iterator<User> iter = userList.iterator(); iter.hasNext(); ) {
			user = iter.next(); 
			jsonUsers = "{username:'" + user.getUsername() + "', score:'" + user.getScore() + "'}," + jsonUsers;
		}
		jsonUsers = "{users:[" + jsonUsers;
		
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonUsers; 
	}
	
	
	public AppDao getAppDao() {
		return appDao;
	}

	@Resource
	public void setAppDao(AppDao appDao) {
		this.appDao = appDao;
	}

	public User getUser() {
		return user;
	}

	@Resource
	public void setUser(User user) {
		this.user = user;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	@Resource
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public App getApp() {
		return app;
	}

	@Resource
	public void setApp(App app) {
		this.app = app;
	}
	
}
